package view.pane;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import view.Gui;
import controller.GameState;
import controller.human.Action;
import controller.human.User;

@SuppressWarnings("serial")
public class DayPane  extends JPanel {
	Gui gui;
	public DayPane(Gui guiParam, User killed, User handicapped,
			Action handicappAction) {
		this.gui = guiParam;
		String message;
		if (killed == null && handicapped == null) {
			message = "Nothing happened during previous night";
		} else if (killed == null) {
			message = handicapped.getName() + " has his " + 
				(handicappAction==Action.Mute?"mouth":"hand") + 
				" mutilated";
		} else {
			message = killed.getName() + " was killed :(";
		}
		
		JPanel gameStatePanel = new JPanel();
		gameStatePanel.add(new JLabel(message));
		
		Set<User> users = gui.getUsers();
		JPanel usersPanel = new JPanel();
		usersPanel.setLayout(new GridLayout((users.size()+2)/3,3));
		
		for (User user: users) {
			JButton selectButton = new JButton(user.getName());
			
			if (user.isDisplay()) {
				selectButton.addActionListener(new SelectActionListener(user));
			} else {
				selectButton.setEnabled(false);
			}
			
			usersPanel.add(selectButton);
		}
		
		JPanel others = new JPanel();
		others.add(new JButton(new AbstractAction("Go to vote") {
			public void actionPerformed(ActionEvent arg0) {
				if (gui.existProposal()) {
					gui.changeState(GameState.Voting);
					gui.updatePane(new VotingPane(gui));
				}
			}
		}));
		
		this.setLayout(new GridLayout(3,1));
		this.add(gameStatePanel);
		this.add(usersPanel);
		this.add(others);
	}
	
	private class SelectActionListener implements ActionListener {
		User user;
		User proposed = null;
		SelectActionListener(User user) {
			this.user = user;
		}
		
		public void actionPerformed(ActionEvent e) {
			Set<User> users = gui.getUsers();
			String[] userStrings = new String[users.size()];
			final List<User> list = new ArrayList<User>(users);
			for (int i=0;i<list.size(); i++) {
				userStrings[i] = list.get(i).getName();
			}
			final JComboBox inputs = new JComboBox(userStrings);
			inputs.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
			        JComboBox inputs = (JComboBox)e.getSource();
			        int userIndex = inputs.getSelectedIndex();
			        proposed = list.get(userIndex);
			        System.out.println("DayPane line 96? : selection changed to " + proposed.getName());
			    }
			});
			
			if (JOptionPane.showConfirmDialog(null, inputs, "Select attribute", JOptionPane.OK_CANCEL_OPTION)==0) {
				gui.addProposal(user, proposed);
			} else {
				//
			}	
		}
	}	
	

}
